﻿using System;
using System.Drawing;
using System.Collections;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace WFFS
{
    /// <summary>
    /// DrawableObjectsCS 的摘要说明。
    /// </summary>
    /// <summary>
    /// 图形枚举。
    /// </summary>
    public enum ImageType
    {

        Path,//路径
        Eraser,
        Text,
        CircleAt,
        Polygon,
        Curve,
        ClosedCurve,
        Beziers,
        Set,
        Pick,
        Empt,
        carDrafting
    }
    //基类
    public abstract class DShape
    {
        public abstract void Draw(Graphics g);
        protected Rectangle bounding;
        protected Point[] pointlist;//点的集合
        public abstract void pointchange(Point[] p);
        public virtual GraphicsPath pathChange(GraphicsPath path, bool pick)
        {
            return null;
        }
        protected Color penColor; //画笔颜色
        protected float penWidth;//画笔宽度
    }

    ///////////////////////////////////////基本图形操作///////////////////////////////////////////////////

    //画路径
    public class DPath : DShape
    {
        protected GraphicsPath Path;
        public DPath(GraphicsPath Path, Color penColor, float penWidth)
        {
            this.Path = Path;
            this.penColor = penColor;
            this.penWidth = penWidth;
        }
        public override void pointchange(Point[] p)
        {
        }
        public override GraphicsPath pathChange(GraphicsPath path, bool pick)
        {
            if (pick == true)
                return this.Path;
            else
            {
                this.Path = path;
                return null;
            }
        }
        public override void Draw(Graphics g)
        {
            using (Pen p = new Pen(penColor, penWidth))
            {
                g.DrawPath(p, Path);
            }
        }
        public GraphicsPath mousePath
        {
            get
            {

                return Path;
            }
        }
    }

    //橡皮擦
    public class DEraser : DShape
    {
        protected Region region;
        protected Color brushColor;
        public DEraser(Point p, Color brushColor, int Size)
        {
            p.Offset(-Size, -Size);
            bounding = new Rectangle(p, new Size(Size * 2, Size * 2));
            this.region = new Region(bounding); ;
            this.brushColor = brushColor;
        }
        public override void pointchange(Point[] p)
        {
        }
        public override void Draw(Graphics g)
        {
            using (Brush b = new SolidBrush(brushColor))
            {
                g.FillRegion(b, region);
            }
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    //图形集合
    public class DShapeList : CollectionBase
    {
        DrawCollection wholeList = new DrawCollection();
        //大小
        public new int Count
        {
            get
            {
                return wholeList.Count;
            }
        }
        //新增
        public void Add(DShape d)
        {
            wholeList.Add(d);
        }
        //删除
        public new void RemoveAt(int i)
        {
            wholeList.RemoveAt(i);
        }

        //索引器
        public DShape this[int newshapeIndex]
        {
            get
            {
                return (DShape)wholeList[newshapeIndex];
            }
            set
            {
                wholeList[newshapeIndex] = value;
            }
        }

        public void DrawList(Graphics g)
        {   //绘制图形
            try
            {
                int count=wholeList.Count;
                for (int i = 0; i < count;i++ )
                {
                    DShape d=wholeList[i];
                       d.Draw(g);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("绘制图形失败:"+e.Message);
            }
        }
    }
    //集合类
    class DrawCollection : CollectionBase
    {
        public void Add(DShape d)
        {
            List.Add(d);
        }

        public new void RemoveAt(int i)
        {
            List.RemoveAt(i);
        }
        public DShape this[int shapeIndex]
        {
            get
            {
                return (DShape)List[shapeIndex];
            }
            set
            {
                List[shapeIndex] = value;
            }
        }
    }
   
}
